﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Liger.Data;
using Liger.Model;

namespace Test
{
    public class NorthwindCUDTest : BaseTest
    {
        public NorthwindCUDTest(DbContext db)
            : base(db)
        {
        }

        /// <summary>
        /// 测试 insert 没有返回主键值(因为没有标示列)
        /// </summary>
        public void TestInsertCustomerNoResult()
        {
            var entity = new Customers
            {
                CustomerID = "XXXX1",
                CompanyName = "Company1",
                ContactName = "Contact1",
                ContactTitle = "ContactTitle1111",
                City = "Seattle",
                Country = "USA"
            };
            var result = Db.Insert<Customers>(entity);
            this.AssertReturn(result > 0);
        }
        /// <summary>
        /// 测试 删除 自定义条件
        /// </summary>
        public void TestDeleteCustomer()
        { 
            var result = Db.Delete<Customers>(Customers._.CustomerID == "XXXX1");
            this.AssertReturn(result > 0);
        }


        /// <summary>
        /// 测试 新增 返回主键值(标示列的值)
        /// </summary>
        public void TestInsertCategoriesWithResult()
        {
            var entity = new Categories
            {
                 CategoryName = "分类二",
                 Description = "描述信息" 
            };
            var result = Db.Insert<Categories>(entity);
            this.AssertReturn(result > 8);
        }


        /// <summary>
        /// 测试 更新
        /// 只更新Attach后修改过的字段
        /// </summary>
        public void TestUpdateCategories()
        {
            var entity = Db.From<Categories>().Where(Categories._.CategoryName == "分类二").ToFirst();
            entity.Attach();
            entity.Description = "描述信息 改变...";
            var result = Db.Update<Categories>(entity);
            this.AssertReturn(result > 0);
        }

        /// <summary>
        /// 测试 更新  entity,where方式
        /// 只更新Attach后修改过的字段
        /// </summary>
        public void TestUpdateCategories_Entity()
        {
            var entity = new Categories();
            entity.Attach();
            entity.Description = "描述信息 改变值111...";
            var result = Db.Update<Categories>(entity, Categories._.CategoryName == "分类二");
            this.AssertReturn(result > 0);
        }

        /// <summary>
        /// 测试 更新  Dictionary,where方式
        /// 只更新指定的字段
        /// </summary>
        public void TestUpdateCategories_Dictionary()
        { 
            var result = Db.Update<Categories>(new Dictionary<Field, object>()
            {
                {Categories._.Description,"描述信息 改变值222..."}
            }, Categories._.CategoryName == "分类二");
            this.AssertReturn(result > 0);
        }


        /// <summary>
        /// 测试 更新  field,value,where方式
        /// 只更新一个字段
        /// </summary>
        public void TestUpdateCategories_FieldValue()
        {
            var result = Db.Update<Categories>(Categories._.Description, "描述信息 改变值333...", Categories._.CategoryName == "分类二");
            this.AssertReturn(result > 0);
        }


        /// <summary>
        /// 测试 删除 传入entity方式
        /// </summary>
        public void TestDeleteCategories_Entity()
        {
            var entity = Db.From<Categories>().Where(Categories._.CategoryName == "分类二").ToFirst();
            var result = Db.Delete<Categories>(entity);
            this.AssertReturn(result > 0); 
        }


        /// <summary>
        /// 测试 更新 日期字段
        /// </summary>
        public void TestUpdateOrders_DateTime()
        {
            var entity = Db.From<Orders>().ToFirst();
            entity.Attach();
            entity.OrderDate = entity.OrderDate.Value.AddDays(1);
            var result = Db.Update<Orders>(entity);
            this.AssertReturn(result > 0);
        }

        /// <summary>
        /// 测试 更新 日期字段(字符串)
        /// </summary>
        public void TestUpdateOrders_DateTime2()
        {
            var entity = Db.From<Orders>().ToFirst();
            var value = entity.OrderDate.Value.AddDays(-1).ToString("yyyy-MM-dd");
            var result = Db.Update<Orders>(Orders._.OrderDate, value, DataHelper.GetPrimaryKeyWhere(entity));
            this.AssertReturn(result > 0);
        }
    }
}
